/**
 * Created by zzg on 2017/4/26.
 */
(function ($, window, document) {
    var page = {
        "pageId": "page",
        "url": null,
        "pageItem": 5,//最多显示的页码个数
        "pageIndex": 1,
        "total": 0,
        "pageSize": 10,//每一页显示的内容条数
        "init": function (index, size, options) {
            var that = this;
            $("div.refresh_tit>i").addClass("icon-spin");
            var data = {index: index, size: size};
            if(typeof options != "undefined" && typeof options.filters != "undefined" && options.filters != null) {
                data['filters'] = options.filters;
            }
            $.get($("ul.page").attr("data-url"), data, function (res) {
                if (res.code == 0) {
                    that.pageIndex = res.page;
                    that.pageSize = res.size;
                    that.total = res.total;
                    $("div.list-div tbody").empty();
                    $("div.refresh_span>.rows").text(res.rows);
                    if(res.rows == 0) {
                        var cols = $("div.list-div tbody").parent().attr("data-cols");
                        $("div.list-div tbody").html('<tr class="tr_bg_blue"><td class="no-records" colspan="'+cols+'">没有找到任何记录</td></tr>');
                    }else
                        $("#table_template").tmpl(res.data).appendTo("div.list-div tbody");
                    page.initPage(res.rows, res.page);
                    setTimeout(function () {
                        $("div.refresh_tit>i").removeClass("icon-spin")
                    }, 1000);
                } else
                    alert(res.message);
            });
            return this;
        },
        /**
         * 初始化数据处理
         * @param rows 列表总量
         * @param currentPage 当前页
         */
        "initPage": function (rows, currentPage) {
            var pageItem = page.pageItem;
            if (pageItem != null && pageItem > 0 && pageItem != "") {
                page.pageItem = pageItem;
            }
            var pageSize = page.pageSize;
            if (pageSize != null && pageSize > 0 && pageSize != "") {
                page.pageSize = pageSize;
            }
            page.pageSize = pageSize;
            if (rows < 0) {
                rows = 0;
            }
            if (rows <= 0) {
                rows = 1;
            }
            page.setPageListCount(rows, currentPage);
        },
        /**
         * 初始化分页界面
         * @param rows 列表总量
         */
        "initWithUl": function (rows, currentPage) {
            var appendStr = page.getPageListModel(page.total, currentPage);
            $("#" + page.pageId).html(appendStr);
        },
        /**
         * 设置列表总量和当前页码
         * @param rows 列表总量
         * @param currentPage 当前页码
         */
        "setPageListCount": function (rows, currentPage) {
            rows = parseInt(rows);
            currentPage = parseInt(currentPage);
            page.initWithUl(rows, currentPage);
            page.initPageEvent(rows);
        },
        "initPageEvent": function (rows) {
            $("#" + page.pageId + ">li[class='pageItem']").on("click", function () {
                page.init($(this).attr("page-data"), page.pageSize, getFilters());
            });
        },
        "getPageListModel": function (pageCount, currentPage) {
            var prePage = currentPage - 1;
            var nextPage = currentPage + 1;
            var prePageClass = "pageItem";
            var nextPageClass = "pageItem";
            if (prePage <= 0) {
                prePageClass = "pageItemDisable";
            }
            if (nextPage > pageCount) {
                nextPageClass = "pageItemDisable";
            }
            var appendStr = "";
            appendStr += "<li class='" + prePageClass + "' page-data='1' page-rel='firstpage'>首页</li>";
            appendStr += "<li class='" + prePageClass + "' page-data='" + prePage + "' page-rel='prepage'>上一页</li>";
            var miniPageNumber = 1;
            if (currentPage - parseInt(page.pageItem / 2) > 0 && currentPage + parseInt(page.pageItem / 2) <= pageCount) {
                miniPageNumber = currentPage - parseInt(page.pageItem / 2);
            } else if (currentPage - parseInt(page.pageItem / 2) > 0 && currentPage + parseInt(page.pageItem / 2) > pageCount) {
                miniPageNumber = pageCount - page.pageItem + 1;
                if (miniPageNumber <= 0) {
                    miniPageNumber = 1;
                }
            }
            var showPageNum = parseInt(page.pageItem);
            if (pageCount < showPageNum) {
                showPageNum = pageCount;
            }
            for (var i = 0; i < showPageNum; i++) {
                var pageNumber = miniPageNumber++;
                var itemPageClass = "pageItem";
                if (pageNumber == currentPage) {
                    itemPageClass = "pageItemActive";
                }

                appendStr += "<li class='" + itemPageClass + "' page-data='" + pageNumber + "' page-rel='itempage'>" + pageNumber + "</li>";
            }
            appendStr += "<li class='" + nextPageClass + "' page-data='" + nextPage + "' page-rel='nextpage'>下一页</li>";
            appendStr += "<li class='" + nextPageClass + "' page-data='" + pageCount + "' page-rel='lastpage'>尾页</li>";
            appendStr += "<li class='jumpText'><input type='text' class='jump_input' ></li>";
            appendStr += "<li class='jumpBtn'>跳转</li>";
            return appendStr;

        },
        "reload": function () {
            page.init(page.pageIndex, page.pageSize);
        },
        setFilters:function(){}
    }

    //刷新
    $(document).on("click", "div.refresh_tit>i", function () {
        page.reload();
    });

    //搜索
    $(document).on("click", "button.search_btn", function () {
        page.init(1, page.pageSize, getFilters());
    });
    //监控输入的是否为数字
    $(document).on("input propertychange", "input.jump_input", function () {
        if(isNaN($(this).val())) $(this).val("");
    });
    $(document).on("click", "li.jumpBtn", function () {
        var inpt = $("input.jump_input").val().trim();
        if(inpt == ""){
            alert("请输入要跳转的页数!");
        }else {
            var index = parseInt(inpt);
            if(index > page.total)
                alert("您输入的页数已经超过了最后一页!");
            else
                page.init(index, page.pageSize);
            $("input.jump_input").val("");
        }
    });

    //获取条件value
    var getFilters = function(){
        var json = null;
        if($("input#keywords").length !=0) {
            var keyValue = $("input#keywords").val().trim();
            json = {keyValue: keyValue};
        }
        if($("#tjType").length !=0)
            json['tjType'] = $("#tjType").val();
        if($("#tjKey").length !=0)
            json['tjKey'] = $("#tjKey").val();
        if(json == null) return undefined;
        return {filters:JSON.stringify(json)};
    }
    //删除数据
    $(document).on("click", "a.btn_trash", function(){
        if(confirm($(this).attr("tips"))){
            $.post($(this).attr("data-url"), function(res){
                if(res.code == 0)
                    page.init(page.pageIndex, page.pageSize, getFilters());
                else
                    alert(res.message);
            });
        }
    });
    window.page = page;
})(jQuery, window, document);